﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

public partial class Main_Inventory_OnlineRequestSlipEdit : System.Web.UI.Page
{
    Datautility DU = new Datautility();
    int Counter = 0;

    List<string> LstItemCode = new List<string>();
    List<string> LstItemCodeExtender = new List<string>();
    List<string> LstItemDesc = new List<string>();
    List<string> LstTotalQty = new List<string>();
    List<string> LstTotalReceive = new List<string>();
    List<string> LstQty = new List<string>();
    List<string> LstPeCode = new List<string>();
    List<string> LstPecodeExtender = new List<string>();
    List<string> LstProductDesc = new List<string>();
    List<string> LstProductModel = new List<string>();
    List<string> LstRo = new List<string>();
    List<string> LstCustomer = new List<string>();
    List<string> LstProjectName = new List<string>();

    protected override void LoadViewState(object savedState)
    {
        base.LoadViewState(savedState);
        LoadViewState();
    }
    private void LoadViewState()
    {
        LstItemCode = (List<string>)ViewState["LstItemCode"];
        LstItemCodeExtender = (List<string>)ViewState["LstItemCodeExtender"];
        LstItemDesc = (List<string>)ViewState["LstItemDesc"];
        LstTotalQty = (List<string>)ViewState["LstTotalQty"];
        LstTotalReceive = (List<string>)ViewState["LstTotalReceive"];
        LstQty = (List<string>)ViewState["LstQty"];
        LstPeCode = (List<string>)ViewState["LstPeCode"];
        LstPecodeExtender = (List<string>)ViewState["LstPecodeExtender"];
        LstProductDesc = (List<string>)ViewState["LstProductDesc"];
        LstProductModel = (List<string>)ViewState["LstProductModel"];
        LstRo = (List<string>)ViewState["LstRo"];
        LstCustomer = (List<string>)ViewState["LstCustomer"];
        LstProjectName = (List<string>)ViewState["LstProjectName"];
        if (Session["hidCounter"] != null)
        {
            int count = Convert.ToInt32(Session["hidCounter"].ToString());
            for (int i = 0; i < count; i++)
            {
                HtmlTableRow htrow = new HtmlTableRow();

                HtmlTableCell htcell1 = new HtmlTableCell();
                TextBox txtitemcode = new TextBox();
                txtitemcode.ID = LstItemCode[i].ToString();
                txtitemcode.Width = Unit.Percentage(96);
                txtitemcode.AutoPostBack = true;
                txtitemcode.TextChanged += new EventHandler(txtitemcode_TextChanged);
                htcell1.Controls.Add(txtitemcode);

                //Ajax Control
                AjaxControlToolkit.AutoCompleteExtender ExtenderitemCode = new AjaxControlToolkit.AutoCompleteExtender();
                ExtenderitemCode.ID = LstItemCodeExtender[i].ToString();
                ExtenderitemCode.ServiceMethod = "GetItemCode";
                ExtenderitemCode.MinimumPrefixLength = 1;
                ExtenderitemCode.CompletionInterval = 0;
                ExtenderitemCode.CompletionListCssClass = "AutoExtender";
                ExtenderitemCode.CompletionListItemCssClass = "AutoExtenderList";
                ExtenderitemCode.CompletionListHighlightedItemCssClass = "AutoExtenderHighlight";
                ExtenderitemCode.EnableCaching = false;
                ExtenderitemCode.TargetControlID = txtitemcode.ID;
                htcell1.Controls.Add(ExtenderitemCode);

                HtmlTableCell htcell2 = new HtmlTableCell();
                TextBox txtitemdesc = new TextBox();
                txtitemdesc.ID = LstItemDesc[i].ToString();
                txtitemdesc.Width = Unit.Percentage(96);
                txtitemdesc.Enabled = false;
                txtitemdesc.ForeColor = System.Drawing.Color.Black;
                htcell2.Controls.Add(txtitemdesc);
                //LstItemDesc.Add(txtitemdesc.ID);

                HtmlTableCell htcell10 = new HtmlTableCell();
                Label lblTotalqty = new Label();
                lblTotalqty.ID = LstTotalQty[i].ToString();
                lblTotalqty.Width = Unit.Percentage(99);
                lblTotalqty.Text = "0";
                lblTotalqty.CssClass = "labelText";
                //hidTotalqty.Attributes.Add("onKeyPress", "return isNumberKey(this.id,event)");
                htcell10.Controls.Add(lblTotalqty);
                //LstAvailQty.Add(lblTotalqty.ID);
                HiddenField hidTotalReceiveId = new HiddenField();
                hidTotalReceiveId.ID = LstTotalReceive[i].ToString();
                htcell10.Controls.Add(hidTotalReceiveId);
                //LstHiddenReqID.Add(hidTotalReceiveId.ID);

                HtmlTableCell htcell3 = new HtmlTableCell();
                TextBox txtqty = new TextBox();
                txtqty.ID = LstQty[i].ToString();
                txtqty.Width = Unit.Percentage(96);
                txtqty.Attributes.Add("onKeyPress", "return isNumberKey(this.id,event)");
                txtqty.Attributes.Add("onblur", "return checkqty();");
                htcell3.Controls.Add(txtqty);
                //LstQty.Add(txtqty.ID);

                HtmlTableCell htcell4 = new HtmlTableCell();
                TextBox txtpecode = new TextBox();
                txtpecode.ID = LstPeCode[i].ToString();
                txtpecode.Width = Unit.Percentage(96);
                txtpecode.AutoPostBack = true;
                txtpecode.TextChanged += new EventHandler(txtpecode_TextChanged);
                htcell4.Controls.Add(txtpecode);

                //Ajax Control
                AjaxControlToolkit.AutoCompleteExtender ExternderpeCode = new AjaxControlToolkit.AutoCompleteExtender();
                ExternderpeCode.ID = LstPecodeExtender[i].ToString();
                ExternderpeCode.ServiceMethod = "GetPeCode";
                ExternderpeCode.MinimumPrefixLength = 1;
                ExternderpeCode.CompletionInterval = 0;
                ExternderpeCode.CompletionListCssClass = "AutoExtender";
                ExternderpeCode.CompletionListItemCssClass = "AutoExtenderList";
                ExternderpeCode.CompletionListHighlightedItemCssClass = "AutoExtenderHighlight";
                ExternderpeCode.EnableCaching = false;
                ExternderpeCode.TargetControlID = txtpecode.ID;
                htcell4.Controls.Add(ExternderpeCode);

                //LstPeCode.Add(txtpecode.ID);

                HtmlTableCell htcell5 = new HtmlTableCell();
                TextBox txtproductdesc = new TextBox();
                txtproductdesc.ID = LstProductDesc[i].ToString();
                txtproductdesc.Width = Unit.Percentage(96);
                txtproductdesc.Enabled = false;
                txtproductdesc.ForeColor = System.Drawing.Color.Black;
                htcell5.Controls.Add(txtproductdesc);
                //LstProductDesc.Add(txtproductdesc.ID);

                HtmlTableCell htcell6 = new HtmlTableCell();
                TextBox txtproductmodel = new TextBox();
                txtproductmodel.ID = LstProductModel[i].ToString();
                txtproductmodel.Width = Unit.Percentage(96);
                txtproductmodel.Enabled = false;
                txtproductmodel.ForeColor = System.Drawing.Color.Black;
                htcell6.Controls.Add(txtproductmodel);
                //LstProductModel.Add(txtproductmodel.ID);

                HtmlTableCell htcell7 = new HtmlTableCell();
                TextBox txtro = new TextBox();
                txtro.ID = LstRo[i].ToString();
                txtro.Width = Unit.Percentage(96);
                htcell7.Controls.Add(txtro);
                //LstRo.Add(txtro.ID);

                HtmlTableCell htcell8 = new HtmlTableCell();
                DropDownList ddlcustomer = new DropDownList();
                ddlcustomer.ID = LstCustomer[i].ToString();
                ddlcustomer.Width = Unit.Percentage(98);
                ddlcustomer.CssClass = "dropdown";
                htcell8.Controls.Add(ddlcustomer);
                //LstCustomer.Add(ddlcustomer.ID);

                HtmlTableCell htcell9 = new HtmlTableCell();
                DropDownList ddlprojectname = new DropDownList();
                ddlprojectname.ID = LstProjectName[i].ToString();
                ddlprojectname.Width = Unit.Percentage(98);
                ddlprojectname.CssClass = "dropdown";
                htcell9.Controls.Add(ddlprojectname);
                //LstProjectName.Add(ddlprojectname.ID);

                htrow.Cells.Add(htcell1);
                htrow.Cells.Add(htcell2);
                htrow.Cells.Add(htcell10);
                htrow.Cells.Add(htcell3);
                htrow.Cells.Add(htcell4);
                htrow.Cells.Add(htcell5);
                htrow.Cells.Add(htcell6);
                htrow.Cells.Add(htcell7);
                htrow.Cells.Add(htcell8);
                htrow.Cells.Add(htcell9);

                tblOnlineRequestSlip.Rows.Add(htrow);
            }
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userid"] == null)
        {
            Response.Redirect("~/SessionExpired.aspx");
        }
        if (!IsPostBack)
        {
            if (Request.QueryString["id"] != null)
            {
                GetDataToOnlineRequestSlip();
                lblmsg.Visible = false;
            }
        }
    }

    [System.Web.Script.Services.ScriptMethod()]
    [System.Web.Services.WebMethod]
    public static List<string> GetItemCode(string prefixText)
    {
        Datautility db = new Datautility();
        DataTable dt = db.getdata("select ITMCODE from IVR_ITEM where itmcode like '%" + prefixText + "%'");
        List<string> ItemCode = new List<string>();
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            ItemCode.Add(Convert.ToString(dt.Rows[i]["ITMCODE"]));
        }
        return ItemCode;
    }

    [System.Web.Script.Services.ScriptMethod()]
    [System.Web.Services.WebMethod]
    public static List<string> GetPeCode(string prefixText)
    {
        Datautility db = new Datautility();
        DataTable dt = db.getdata("select PecCode from ACC_PRODUCT_MASTER where PecCode like '%" + prefixText + "%'");
        List<string> PeCode = new List<string>();
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            PeCode.Add(Convert.ToString(dt.Rows[i]["PecCode"]));
        }
        return PeCode;
    }

    protected void txtitemcode_TextChanged(object sender, EventArgs e)
    {
        TextBox txt = (TextBox)sender;
        string txtid = txt.ID.ToString().Substring(11);
        int rowIndex = Convert.ToInt32(txtid.ToString());
        TextBox txtItemdesc = (TextBox)tblOnlineRequestSlip.Rows[rowIndex - 1].FindControl("txtitemdesc" + rowIndex.ToString());
        TextBox txtItemqty = (TextBox)tblOnlineRequestSlip.Rows[rowIndex - 1].FindControl("txtqty" + rowIndex.ToString());
        Label lblTotalqty = (Label)tblOnlineRequestSlip.Rows[rowIndex - 1].FindControl("lblTotalqty" + rowIndex.ToString());
        HiddenField hidTotalReceiveId = (HiddenField)tblOnlineRequestSlip.Rows[rowIndex - 1].FindControl("hidTotalReceiveId" + rowIndex.ToString());
        if (txt.Text != null)
        {
            string sql = "select ITMDESC,ITMID from IVR_item where ITMCODE='" + txt.Text + "'";
            DataTable DT = DU.getdata(sql);
            if (DT.Rows.Count > 0)
            {
                txtItemdesc.Text = DT.Rows[0]["ITMDESC"].ToString();
                string sqlItem = "select TITID,ITMQTY,coalesce(issueqty,0)as ISSUEQTY from IVR_TRAN_ITEM where ITMID='" + DT.Rows[0]["ITMID"].ToString() + "' and isnull(ITMQTY,'0') > isnull(issueQTY,'0')";
                DataTable dtTranItem = DU.getdata(sqlItem);
                if (dtTranItem.Rows.Count > 0)
                {
                    double qty = 0; string Received = ""; double issueqty = 0;
                    for (int i = 0; i < dtTranItem.Rows.Count; i++)
                    {
                        qty = qty + Convert.ToDouble(dtTranItem.Rows[i]["ITMQTY"].ToString());
                        Received += dtTranItem.Rows[i]["TITID"].ToString() + ",";
                        issueqty = issueqty + Convert.ToDouble(dtTranItem.Rows[i]["ISSUEQTY"].ToString());
                    }
                    lblTotalqty.Text = (Convert.ToDouble(qty) - Convert.ToDouble(issueqty)).ToString();

                    hidTotalReceiveId.Value = Received.Remove(Received.Length - 1);
                    txtItemqty.ToolTip = "Balance Qty (" + string.Format("{0:0.00}", Convert.ToDouble(lblTotalqty.Text)) + ")";
                }
            }
            else
            {
                txt.Text = string.Empty;
                ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "validitemcode();", true);
            }
        }
    }

    protected void txtpecode_TextChanged(object sender, EventArgs e)
    {
        TextBox txt = (TextBox)sender;
        string txtid = txt.ID.ToString().Substring(9);
        int rowIndex = Convert.ToInt32(txtid.ToString());
        TextBox txtproductdesc = (TextBox)tblOnlineRequestSlip.Rows[rowIndex - 1].FindControl("txtproductdesc" + rowIndex.ToString());
        TextBox txtproductmodel = (TextBox)tblOnlineRequestSlip.Rows[rowIndex - 1].FindControl("txtproductmodel" + rowIndex.ToString());
        if (txt.Text != null)
        {
            string sql = "select ProjectModel, ProjectDesc from ACC_PRODUCT_MASTER where PecCode='" + txt.Text + "'";
            DataTable DT = DU.getdata(sql);
            if (DT.Rows.Count > 0)
            {
                txtproductdesc.Text = DT.Rows[0]["ProjectDesc"].ToString();
                txtproductmodel.Text = DT.Rows[0]["ProjectModel"].ToString();
            }
            else
            {
                txt.Text = string.Empty;
                ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "validPECode();", true);
            }
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        int p = 1;
        int Count = Convert.ToInt32(Session["hidCounter"].ToString());
        if (txtreqdate.Text == "")
        {
            txtreqdate.Text = System.DateTime.Now.ToShortDateString();
        }
        try
        {
            SqlParameter[] param = {new SqlParameter("@reqdate",Convert.ToDateTime(txtreqdate.Text)),
                                    new SqlParameter("@slipno",txtslipno.Text),
                                    new SqlParameter("@username",txtusername.Text),
                                    new SqlParameter("@remarks",txtremarks.Text),
                                   new SqlParameter("@regid",hidregid.Value)
                                   };
            int insert = DU.executeprocedure("Usp_IVR_UpdateRequestSlip", param);
            if (insert > 0)
            {
                for (int i = 1; i <= Count; i++)
                {
                    TextBox txtItemCode = (TextBox)tblOnlineRequestSlip.Rows[i].FindControl("txtitemcode" + i.ToString());
                    TextBox txtItemDesc = (TextBox)tblOnlineRequestSlip.Rows[i].FindControl("txtitemdesc" + i.ToString());
                    Label lblAvailQty = (Label)tblOnlineRequestSlip.Rows[i].FindControl("lblTotalqty" + i.ToString());
                    TextBox txtQty = (TextBox)tblOnlineRequestSlip.Rows[i].FindControl("txtqty" + i.ToString());
                    TextBox txtPeCode = (TextBox)tblOnlineRequestSlip.Rows[i].FindControl("txtpecode" + i.ToString());
                    TextBox txtProductDesc = (TextBox)tblOnlineRequestSlip.Rows[i].FindControl("txtproductdesc" + i.ToString());
                    TextBox txtProductModel = (TextBox)tblOnlineRequestSlip.Rows[i].FindControl("txtproductmodel" + i.ToString());
                    TextBox txtRo = (TextBox)tblOnlineRequestSlip.Rows[i].FindControl("txtro" + i.ToString());
                    DropDownList ddlCustomer = (DropDownList)tblOnlineRequestSlip.Rows[i].FindControl("ddlcustomer" + i.ToString());
                    DropDownList ddlProjectname = (DropDownList)tblOnlineRequestSlip.Rows[i].FindControl("ddlprojectname" + i.ToString());

                    if (txtItemCode.Text != "" || txtQty.Text != "" || txtPeCode.Text != "" || txtRo.Text != "" || Convert.ToInt32(ddlCustomer.SelectedValue) > 0 || Convert.ToInt32(ddlProjectname.SelectedValue) > 0)
                    {
                        if (txtQty.Text != "" && txtPeCode.Text != "" && txtRo.Text != "" && Convert.ToInt32(ddlCustomer.SelectedValue) > 0 && Convert.ToInt32(ddlProjectname.SelectedValue) > 0)
                        {
                            SqlParameter[] paramdetail = { new SqlParameter("@ItemCode",txtItemCode.Text.Trim()),
                                                    new SqlParameter("@ItemDesc",txtItemDesc.Text.Trim()),
                                                    new SqlParameter("@qty",txtQty.Text.Trim()),
                                                    new SqlParameter("@PeCode",txtPeCode.Text.Trim()),
                                                    new SqlParameter("@ProductDesc",txtProductDesc.Text.Trim()),
                                                    new SqlParameter("@ProductModel",txtProductModel.Text.Trim()),
                                                    new SqlParameter("@Ro",txtRo.Text.Trim()),
                                                    new SqlParameter("@Customer",ddlCustomer.SelectedValue),
                                                    new SqlParameter("@Project",ddlProjectname.SelectedValue),
                                                    new SqlParameter("@regid",hidregid.Value),
                                                    new SqlParameter("@id",Request.QueryString["id"].ToString())
                                                 };
                            int insertdetail = DU.executeprocedure("Usp_IVR_UpdateRequestSlipDetails", paramdetail);
                            if (insertdetail > 0)
                            {
                                p++;
                                lblmsg.Text = "Requested Item Updated...";
                                lblmsg.Visible = true;
                            }
                        }
                        else
                        {
                            ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "CheckRowForOnlineRequestSlip();", true);
                            p = 1;
                        }
                    }
                    else
                    {
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "rowEmpty();", true);
                    }
                }
            }
            if (p == 1)
            {
                //string strreqsql = "Select Ident_Current('IVR_OnlineRegistrationSlip') as ReqId";
                //DataTable DTDelete = DU.getdata(strreqsql);
                //if (DTDelete.Rows.Count > 0)
                //{
                //    SqlParameter[] paramreq = { new SqlParameter("@regid", DTDelete.Rows[0]["ReqId"].ToString()) };
                //    int rollback = DU.executeprocedure("IVR_DeleteOnlineRequestSlip", paramreq);
                //    if (rollback > 0)
                //    {
                //    }
                //}
            }
            else
            {
                if (!IsPostBack)
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "rowEmpty();", true);
                }
            }
            ClearControl();
        }
        catch (Exception ex)
        {
            //lblmsg.Text = ex.Message;
            //lblmsg.Visible = true;
        }
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("OnlineRequestSlip.aspx?Mode=View");
    }

    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        Addrow();
    }

    protected void Addrow()
    {
        Counter++;

        HtmlTableRow htrow = new HtmlTableRow();

        HtmlTableCell htcell1 = new HtmlTableCell();
        TextBox txtitemcode = new TextBox();
        txtitemcode.ID = "txtitemcode" + Counter.ToString();
        txtitemcode.Width = Unit.Percentage(96);
        txtitemcode.AutoPostBack = true;
        txtitemcode.TextChanged += new EventHandler(txtitemcode_TextChanged);
        htcell1.Controls.Add(txtitemcode);
        LstItemCode.Add(txtitemcode.ID);
        //Ajax Control
        AjaxControlToolkit.AutoCompleteExtender ExtenderitemCode = new AjaxControlToolkit.AutoCompleteExtender();
        ExtenderitemCode.ID = "ExtenderitemCode" + Counter.ToString();
        ExtenderitemCode.ServiceMethod = "GetItemCode";
        ExtenderitemCode.MinimumPrefixLength = 1;
        ExtenderitemCode.CompletionInterval = 0;
        ExtenderitemCode.CompletionListCssClass = "AutoExtender";
        ExtenderitemCode.CompletionListItemCssClass = "AutoExtenderList";
        ExtenderitemCode.CompletionListHighlightedItemCssClass = "AutoExtenderHighlight";
        ExtenderitemCode.EnableCaching = false;
        ExtenderitemCode.TargetControlID = txtitemcode.ID;
        htcell1.Controls.Add(ExtenderitemCode);
        LstItemCodeExtender.Add(ExtenderitemCode.ID);

        HtmlTableCell htcell2 = new HtmlTableCell();
        TextBox txtitemdesc = new TextBox();
        txtitemdesc.ID = "txtitemdesc" + Counter.ToString();
        txtitemdesc.Width = Unit.Percentage(96);
        txtitemdesc.Enabled = false;
        txtitemdesc.ForeColor = System.Drawing.Color.Black;
        htcell2.Controls.Add(txtitemdesc);
        LstItemDesc.Add(txtitemdesc.ID);

        HtmlTableCell htcell10 = new HtmlTableCell();
        Label lblTotalqty = new Label();
        lblTotalqty.ID = "lblTotalqty" + Counter.ToString();
        lblTotalqty.Width = Unit.Percentage(99);
        lblTotalqty.Text = "0";
        lblTotalqty.CssClass = "labelText";
        //hidTotalqty.Attributes.Add("onKeyPress", "return isNumberKey(this.id,event)");
        htcell10.Controls.Add(lblTotalqty);
        LstTotalQty.Add(lblTotalqty.ID);

        HiddenField hidTotalReceiveId = new HiddenField();
        hidTotalReceiveId.ID = "hidTotalReceiveId" + Counter.ToString();
        htcell10.Controls.Add(hidTotalReceiveId);
        LstTotalReceive.Add(hidTotalReceiveId.ID);

        HtmlTableCell htcell3 = new HtmlTableCell();
        TextBox txtqty = new TextBox();
        txtqty.ID = "txtqty" + Counter.ToString();
        txtqty.Width = Unit.Percentage(96);
        txtqty.Attributes.Add("onKeyPress", "return isNumberKey(this.id,event)");
        txtqty.Attributes.Add("onblur", "return checkqty();");
        htcell3.Controls.Add(txtqty);
        LstQty.Add(txtqty.ID);

        HtmlTableCell htcell4 = new HtmlTableCell();
        TextBox txtpecode = new TextBox();
        txtpecode.ID = "txtpecode" + Counter.ToString();
        txtpecode.Width = Unit.Percentage(96);
        txtpecode.AutoPostBack = true;
        txtpecode.TextChanged += new EventHandler(txtpecode_TextChanged);
        htcell4.Controls.Add(txtpecode);
        LstPeCode.Add(txtpecode.ID);

        //Ajax Control
        AjaxControlToolkit.AutoCompleteExtender ExternderpeCode = new AjaxControlToolkit.AutoCompleteExtender();
        ExternderpeCode.ID = "ExternderpeCode" + Counter.ToString();
        ExternderpeCode.ServiceMethod = "GetPeCode";
        ExternderpeCode.MinimumPrefixLength = 1;
        ExternderpeCode.CompletionInterval = 0;
        ExternderpeCode.CompletionListCssClass = "AutoExtender";
        ExternderpeCode.CompletionListItemCssClass = "AutoExtenderList";
        ExternderpeCode.CompletionListHighlightedItemCssClass = "AutoExtenderHighlight";
        ExternderpeCode.EnableCaching = false;
        ExternderpeCode.TargetControlID = txtpecode.ID;
        htcell4.Controls.Add(ExternderpeCode);
        LstPecodeExtender.Add(ExternderpeCode.ID);

        HtmlTableCell htcell5 = new HtmlTableCell();
        TextBox txtproductdesc = new TextBox();
        txtproductdesc.ID = "txtproductdesc" + Counter.ToString();
        txtproductdesc.Width = Unit.Percentage(96);
        txtproductdesc.Enabled = false;
        txtproductdesc.ForeColor = System.Drawing.Color.Black;
        htcell5.Controls.Add(txtproductdesc);
        LstProductDesc.Add(txtproductdesc.ID);

        HtmlTableCell htcell6 = new HtmlTableCell();
        TextBox txtproductmodel = new TextBox();
        txtproductmodel.ID = "txtproductmodel" + Counter.ToString();
        txtproductmodel.Width = Unit.Percentage(96);
        txtproductmodel.Enabled = false;
        txtproductmodel.ForeColor = System.Drawing.Color.Black;
        htcell6.Controls.Add(txtproductmodel);
        LstProductModel.Add(txtproductmodel.ID);

        HtmlTableCell htcell7 = new HtmlTableCell();
        TextBox txtro = new TextBox();
        txtro.ID = "txtro" + Counter.ToString();
        txtro.Width = Unit.Percentage(96);
        htcell7.Controls.Add(txtro);
        LstRo.Add(txtro.ID);

        HtmlTableCell htcell8 = new HtmlTableCell();
        DropDownList ddlcustomer = new DropDownList();
        ddlcustomer.ID = "ddlcustomer" + Counter.ToString();
        ddlcustomer.Width = Unit.Percentage(98);
        ddlcustomer.CssClass = "dropdown";
        htcell8.Controls.Add(ddlcustomer);
        LstCustomer.Add(ddlcustomer.ID);

        HtmlTableCell htcell9 = new HtmlTableCell();
        DropDownList ddlprojectname = new DropDownList();
        ddlprojectname.ID = "ddlprojectname" + Counter.ToString();
        ddlprojectname.Width = Unit.Percentage(98);
        ddlprojectname.CssClass = "dropdown";
        htcell9.Controls.Add(ddlprojectname);
        LstProjectName.Add(ddlprojectname.ID);

        htrow.Cells.Add(htcell1);
        htrow.Cells.Add(htcell2);
        htrow.Cells.Add(htcell10);
        htrow.Cells.Add(htcell3);
        htrow.Cells.Add(htcell4);
        htrow.Cells.Add(htcell5);
        htrow.Cells.Add(htcell6);
        htrow.Cells.Add(htcell7);
        htrow.Cells.Add(htcell8);
        htrow.Cells.Add(htcell9);

        tblOnlineRequestSlip.Rows.Add(htrow);

        ViewState["LstItemCode"] = LstItemCode;
        ViewState["LstItemCodeExtender"] = LstItemCodeExtender;
        ViewState["LstItemDesc"] = LstItemDesc;
        ViewState["LstTotalQty"] = LstTotalQty;
        ViewState["LstTotalReceive"] = LstTotalReceive;
        ViewState["LstQty"] = LstQty;
        ViewState["LstPeCode"] = LstPeCode;
        ViewState["LstPecodeExtender"] = LstPecodeExtender;
        ViewState["LstProductDesc"] = LstProductDesc;
        ViewState["LstProductModel"] = LstProductModel;
        ViewState["LstRo"] = LstRo;
        ViewState["LstCustomer"] = LstCustomer;
        ViewState["LstProjectName"] = LstProjectName;

        Session["hidCounter"] = Counter.ToString();
        BindCustomer(); BindProjectName();
    }
    private void ClearControl()
    {
        int Count = Convert.ToInt32(idvalue.Value);
        for (int i = 1; i <= Count; i++)
        {
            TextBox txtItemCode = (TextBox)tblOnlineRequestSlip.Rows[i - 1].FindControl("txtitemcode" + i.ToString());
            TextBox txtItemDesc = (TextBox)tblOnlineRequestSlip.Rows[i - 1].FindControl("txtitemdesc" + i.ToString());
            Label lblAvailQty = (Label)tblOnlineRequestSlip.Rows[i - 1].FindControl("lblTotalqty" + i.ToString());
            TextBox txtQty = (TextBox)tblOnlineRequestSlip.Rows[i - 1].FindControl("txtqty" + i.ToString());
            TextBox txtPeCode = (TextBox)tblOnlineRequestSlip.Rows[i - 1].FindControl("txtpecode" + i.ToString());
            TextBox txtProductDesc = (TextBox)tblOnlineRequestSlip.Rows[i - 1].FindControl("txtproductdesc" + i.ToString());
            TextBox txtProductModel = (TextBox)tblOnlineRequestSlip.Rows[i - 1].FindControl("txtproductmodel" + i.ToString());
            TextBox txtRo = (TextBox)tblOnlineRequestSlip.Rows[i - 1].FindControl("txtro" + i.ToString());
            DropDownList ddlCustomer = (DropDownList)tblOnlineRequestSlip.Rows[i - 1].FindControl("ddlcustomer" + i.ToString());
            DropDownList ddlProjectname = (DropDownList)tblOnlineRequestSlip.Rows[i - 1].FindControl("ddlprojectname" + i.ToString());
            txtItemCode.Text = "";
            txtItemDesc.Text = "";
            lblAvailQty.Text = "";
            txtQty.Text = "";
            txtPeCode.Text = "";
            txtProductDesc.Text = "";
            txtProductModel.Text = "";
            txtRo.Text = "";
            ddlCustomer.SelectedValue = "0";
            ddlProjectname.SelectedValue = "0";
        }
    }

    protected void BindCustomer()
    {
        string sqlcustomer = "select LNAME,LID from ACC_LEDMAST";
        DataTable dt = DU.getdata(sqlcustomer);
        int count = Convert.ToInt32(Session["hidCounter"].ToString());
        if (dt.Rows.Count > 0)
        {
            for (int i = 1; i <= count; i++)
            {
                DropDownList ddlcust = (DropDownList)tblOnlineRequestSlip.Rows[i].FindControl("ddlcustomer" + i.ToString());
                ddlcust.DataSource = dt;
                ddlcust.DataTextField = "LNAME";
                ddlcust.DataValueField = "LID";
                ddlcust.DataBind();
                ddlcust.Items.Insert(0, new ListItem("-Select Customer-", "0"));
            }
        }
    }

    protected void BindProjectName()
    {
        string Sqlproject = "SELECT PROJECTID,PROJECTNAME  FROM MAS_PROJECT";
        DataTable dt = DU.getdata(Sqlproject);
        int count = Convert.ToInt32(Session["hidCounter"].ToString());
        if (dt.Rows.Count > 0)
        {
            for (int i = 1; i <= count; i++)
            {
                DropDownList ddlproject = (DropDownList)tblOnlineRequestSlip.Rows[i].FindControl("ddlprojectname" + i.ToString());
                ddlproject.DataSource = dt;
                ddlproject.DataTextField = "PROJECTNAME";
                ddlproject.DataValueField = "PROJECTID";
                ddlproject.DataBind();
                ddlproject.Items.Insert(0, new ListItem("- Select Project -", "0"));
            }
        }
    }

    protected void GetDataToOnlineRequestSlip()
    {
        SqlParameter[] param = { new SqlParameter("@id", Request.QueryString["id"].ToString()) };
        DataTable dt = DU.getdatausingprocedure("IVR_OnlineRegistration_Edit", param);
        if (dt.Rows.Count > 0)
        {
            for (int j = 1; j <= dt.Rows.Count; j++)
            {
                Addrow();
            }
            int CountRow = Convert.ToInt32(Session["hidCounter"].ToString());
            txtslipno.Text = dt.Rows[0]["slipno"].ToString();
            txtreqdate.Text = Convert.ToDateTime(dt.Rows[0]["reqdate"].ToString()).ToShortDateString();
            txtusername.Text = dt.Rows[0]["username"].ToString();
            txtremarks.Text = dt.Rows[0]["remarks"].ToString();
            hidregid.Value = dt.Rows[0]["Regid"].ToString();
            for (int i = 1; i <= CountRow; i++)
            {
                TextBox txtItemCode = (TextBox)tblOnlineRequestSlip.Rows[i].FindControl("txtitemcode" + i.ToString());
                TextBox txtItemDesc = (TextBox)tblOnlineRequestSlip.Rows[i].FindControl("txtitemdesc" + i.ToString());
                TextBox txtQty = (TextBox)tblOnlineRequestSlip.Rows[i].FindControl("txtqty" + i.ToString());
                Label LblAvailQty = (Label)tblOnlineRequestSlip.Rows[i].FindControl("lblTotalqty" + i.ToString());
                TextBox txtPeCode = (TextBox)tblOnlineRequestSlip.Rows[i].FindControl("txtpecode" + i.ToString());
                TextBox txtProductDesc = (TextBox)tblOnlineRequestSlip.Rows[i].FindControl("txtproductdesc" + i.ToString());
                TextBox txtProductModel = (TextBox)tblOnlineRequestSlip.Rows[i].FindControl("txtproductmodel" + i.ToString());
                TextBox txtRo = (TextBox)tblOnlineRequestSlip.Rows[i].FindControl("txtro" + i.ToString());
                DropDownList ddlCustomer = (DropDownList)tblOnlineRequestSlip.Rows[i].FindControl("ddlcustomer" + i.ToString());
                DropDownList ddlProjectname = (DropDownList)tblOnlineRequestSlip.Rows[i].FindControl("ddlprojectname" + i.ToString());
                HiddenField hidTotalReceiveId = (HiddenField)tblOnlineRequestSlip.Rows[i].FindControl("hidTotalReceiveId" + i.ToString());

                txtItemCode.Text = dt.Rows[i - 1]["ItemCode"].ToString();
                txtItemDesc.Text = dt.Rows[i - 1]["ItemDesc"].ToString();
                txtQty.Text = dt.Rows[i - 1]["qty"].ToString();
                LblAvailQty.Text = dt.Rows[i - 1]["Qty"].ToString();
                txtPeCode.Text = dt.Rows[i - 1]["PeCode"].ToString();
                txtProductDesc.Text = dt.Rows[i - 1]["ProductDesc"].ToString();
                txtProductModel.Text = dt.Rows[i - 1]["ProductModel"].ToString();
                txtRo.Text = dt.Rows[i - 1]["RO"].ToString();
                ddlCustomer.SelectedValue = dt.Rows[i - 1]["Customer"].ToString();
                ddlProjectname.SelectedValue = dt.Rows[i - 1]["Project"].ToString();
            }
        }
    }
}